/**
 *	角色管理
 *	@auth zongjl
 *	@date 2018-03-06
 */
layui.use(['form', 'layer', 'table', 'laytpl'], function () {

    //声明变量
    var layer = parent.layer === undefined ? layui.layer : top.layer
            , table = layui.table
            , form = layui.form
            , layedit = layui.layedit
            , laytpl = layui.laytpl
            , $ = layui.$;

    //调用API接口渲染TABLE(方法级)
    var tableIns = table.render({
        elem: "#tableList"
        , url: "/manager/adminrole/index"
        , method: 'post'
        , cellMinWidth: 150
        , page: true
        , height: "full-125"
        , limit: 20
        , limits: [20, 30, 40, 50, 60, 70, 80, 90, 100, 150, 200]
        , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', width: 80, title: 'ID', align: 'center', sort: true, fixed: 'left'}
                , {field: 'name', width: 250, title: '角色名称', align: 'center'}
                , {field: 'status', width: 200, title: '状态', align: 'center', templet: function (d) {
                        var str = "";
                        if (d.status == 1) {
                            str = '<span class="layui-btn layui-btn-normal layui-btn-xs">在用</span>';
                        } else {
                            str = '<span class="layui-btn layui-btn-normal layui-btn-xs layui-btn-danger">停用</span>';
                        }
                        return str;
                    }}
                , {field: 'format_add_user', width: 200, title: '创建人', align: 'center'}
                , {field: 'format_add_time', width: 200, title: '添加时间', align: 'center', sort: true}
                , {field: 'format_upd_time', width: 200, title: '更新时间', align: 'center', sort: true}
                , {field: 'sort_order', width: 100, title: '排序', align: 'center'}
                , {fixed: 'right', width: 250, title: '功能操作区', align: 'center', toolbar: '#toolBar'}
            ]]
    });

    //监听工具条
    table.on('tool(tableList)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
        if (layEvent === 'auth') {
            layer.msg('权限设置');

            //角色权限设置
            location.href = "/manager/adminauth/index?type=1&type_id=" + data.id;

        } else if (layEvent === 'del') {
            layer.confirm('您确定要删除吗？删除后将无法恢复！', function (index) {

                $.ajax({
                    url: "/?app=" + APP + "&act=drop",
                    dataType: "json",
                    type: "POST",
                    data: {"id": data.id},
                    beforeSend: function () {
                        layer.msg('正在删除。。。', {
                            icon: 16
                            , shade: 0.01
                            , time: 0
                        });
                    },
                    success: function (res) {
                        if (res.success) {
                            //2秒后关闭
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                //tableIns.reload();
                                obj.del();
                                layer.close(index);
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });

            });
        } else if (layEvent === 'edit') {
            layer.msg('编辑操作');
            edit(data.id, data);
        }
    });

    /**
     * 监听状态开关操作
     */
    form.on('switch(switchTplStatus)', function (obj) {
        layer.tips(this.value + ' ' + this.name + '：' + obj.elem.checked, obj.othis);

        layer.msg('开关checked：' + (this.checked ? 'true' : 'false'), {
            offset: '6px'
        });
        layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)

    });

    //批量删除
    $(".btnDAll").click(function () {
        var checkStatus = table.checkStatus('tableList'),
                data = checkStatus.data,
                ids = [];
        if (data.length > 0) {
            for (var i in data) {
                ids.push(data[i].id);
            }
            var idsStr = ids.join(",");
            layer.confirm('确定删除选中的数据吗？', {icon: 3, title: '提示信息'}, function (index) {

                $.ajax({
                    url: "/?app=" + APP + "&act=batchDrop",
                    dataType: "json",
                    type: "POST",
                    data: {"id": idsStr, "changeAct": 0},
                    beforeSend: function () {
                        layer.msg('正在提交。。。', {
                            icon: 16
                            , shade: 0.01
                            , time: 0
                        });
                        $('#submitForm').attr('disabled', "true");
                    },
                    success: function (res) {
                        $('#submitForm').removeAttr("disabled");
                        layer.closeAll();
                        if (res.success) {
                            //2秒后关闭
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                tableIns.reload();
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
            })
        } else {
            layer.msg("请选择需要删除的数据");
        }
    });

    //搜索功能
    $(".search_btn").on("click", function () {
        if ($(".searchVal").val() != '') {
            table.reload("tableList", {
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        } else {
            layer.msg("请输入搜索的内容");
        }
    });

    //添加
    $(".btnAdd").click(function () {
        edit(0);
    });

    //添加或编辑
    function edit(id, data) {
        var title = '新增角色';
        if (id > 0) {
            title = '修改角色';
        }
        var index = layui.layer.open({
            title: title,
            type: 2,
            area: ["450px", "300px"],
            content: "/manager/adminrole/edit?id=" + id,
            success: function (layero, index) {
                //TODO...
                setTimeout(function () {
                    layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            },
//            end: function () {
//                location.reload();
//            }
        });
    }
    ;

    //监听提交
    form.on('submit(submitForm)', function (data) {
        var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.2});
        $.post(aUrl, data.field, function (data) {
            if (data.success) {
                layer.close(index);
                layer.msg("保存成功！");
                layer.closeAll("iframe");

                //刷新父页面
                parent.location.reload();

                return false;
            } else {
                layer.close(index);
                layer.msg(data.msg);
            }
        }, 'json');

        return false;
    });

});